package com.bruceewu.galleryphoto.page;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;

import com.bruceewu.configor.entity.DisplayItem;
import com.bruceewu.configor.holder.DefaultHolders;
import com.bruceewu.galleryphoto.R;
import com.bruceewu.galleryphoto.base.BaseActivity;
import com.bruceewu.galleryphoto.base.PhotoFragment;
import com.bruceewu.galleryphoto.base.IListConfigor;
import com.bruceewu.galleryphoto.bean.ImageBean;
import com.bruceewu.galleryphoto.utis.ImageChooser;
import com.bruceewu.galleryphoto.utis.ScreenUtils;
import com.bruceewu.galleryphoto.utis.SharedPreferenceHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PhotoActivity extends BaseActivity {
    private static final String DEFAULT_PATH = "/sdcard/DCIM/";

    public static void Open(Context context, String path) {
        ImageChooser.checkPermission(context, () -> {
            Intent intent = new Intent(context, PhotoActivity.class);
            intent.putExtra("path", path);
            context.startActivity(intent);
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.root, PhotoFragment.newInstance(configor()), "PhotoActivity")
                .commitAllowingStateLoss();
    }

    private IListConfigor configor() {

        return new IListConfigor() {
            @Override
            public String title() {
                return "详情页";
            }

            @Override
            public List<DisplayItem> imgs() {
                Pair<String, String> pair = getDirAndLastImg();
                List<ImageBean> imgs = ImageChooser.filterImgs(pair.first, pair.second);
                List<DisplayItem> items = new ArrayList<>();
                for (ImageBean img : imgs) {
                    DisplayItem item = DisplayItem.newItem(DefaultHolders.Image.showType());
                    item.setShowData(img.path());
                    item.setWidth(img.width());
                    item.setHeight(img.height());
                    item.putExtra("info", img);
                    items.add(item);
                }
                return items;
            }
        };
    }

    private Pair<String, String> getDirAndLastImg() {
        String path = getIntent().getStringExtra("path");
        String fixedDir = null;
        String fixedPath = null;
        if (TextUtils.isEmpty(path)) {//取上次播放记录
            String localDir = SharedPreferenceHelper.getLastDir();
            String localFile = SharedPreferenceHelper.getLastFile();
            if (TextUtils.isEmpty(localDir)) {
                fixedDir = DEFAULT_PATH;
            } else {
                fixedDir = localDir;
            }
            fixedPath = localFile;
        } else {//用path为准
            File file = new File(path);
            if (file.isFile()) {
                fixedDir = file.getParentFile().getAbsolutePath();
                fixedPath = path;
            } else {
                fixedDir = path;
                fixedPath = null;
            }
        }
        SharedPreferenceHelper.putLastDir(fixedDir);
        SharedPreferenceHelper.putLastFile(fixedPath);
        return new Pair<>(fixedDir, fixedPath);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ScreenUtils.screenLightOn(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        ScreenUtils.screenLightOff(this);
    }
}
